<template>
  <view>
    <view class="search-box">
      <uni-search-bar @input="input" :radius="100" cancelButton="none"></uni-search-bar>
    </view>
    <!-- 搜索建议列表 -->
    <view class="sugg-list" v-if="searchResult.length!=0">
      <view class="sugg-item" v-for="(item,i) in searchResult" :key="i" @click="gotoDetail(item.goods_id)">
        <view class="goods-name">
          {{item.goods_name}}
        </view>
        <uni-icons type="arrowright" size="18"></uni-icons>
      </view>
    </view>
    <!-- 搜索历史 -->
    <view class="history-box" v-else>
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="16" @click="clean"></uni-icons>
      </view>
      <view class="history-list">
        <uni-tag :text="item" v-for="(item,i) in histories" :k="i" @click="gotoGoodsList(item)"></uni-tag>
      </view>
    </view>
    <!-- --是是是 -->
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer: null,
        kw: '',
        // 搜索列表数据
        searchResult: [],
        // 搜索历史
        historyList: []
      };
    },
    methods: {
      // 获取输入框的值
      input(e) {
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          this.kw = e.value
          this.getSearchList()
        }, 500)
      },
      // 获取搜索列表
      async getSearchList() {
        console.log(this.kw.length)
        if (this.kw.length === 0) {
          this.searchResult = []

          return
        }
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/qsearch', {
          query: this.kw
        })
        console.log(res)
        if (res.meta.status !== 200) return uni.$showMsg()

        this.searchResult = res.message
        if (this.searchResult.length == 0) {
          uni.$showMsg('找不到相关产品')
        }
        this.svaeSearchHistory()
      },
      // 搜索列表的跳转
      gotoDetail(id) {
        console.log(id)
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + id
        })
      },
      // 保存搜索历史记录
      svaeSearchHistory() {
        // this.historyList.push(this.kw)
        // 利用set数据类型对数组去重
        // 1 将历史记录数组转化为set对象
        const set = new Set(this.historyList)
        // 如果输入框的值以及在历史记录数组中出现过 要保证历史记录不能重复而且新的历史记录要永远排在前面 unshift会改变索引 所有慎用！
        //2 先根据输入框的值移除旧的元素 如果没有往末尾添加
        set.delete(this.kw)
        // 3 从尾部追加新元素 而翻转后的顺序就是搜索的历史顺序
        set.add(this.kw)
        // 4 将set转化为数组 然后从新存储
        this.historyList = Array.from(set)
        console.log(set)
        // 持久化存储搜索历史记录
        uni.setStorageSync('kw', JSON.stringify(this.historyList))
      },
      // 清空搜索历史记录
      clean() {
        console.log(111)
        this.historyList = []
        uni.setStorageSync('kw', '[]')
      },
      // 点击搜索历史跳转
      gotoGoodsList(kw) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?query=' + kw
        })
      }
    },
    computed: {
      histories() {
        return [...this.historyList].reverse()
      }
    },
    onLoad() {
      // 初始化本地历史记录数据
      this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
    },
  }
</script>

<style lang="scss">
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
  }

  .sugg-list {
    padding: 0 5px;

    .sugg-item {
      font-size: 12px;
      padding: 13px 0;
      display: flex;
      align-items: center;
      justify-content: space-between;
      border-bottom: 1px solid #efefef;

      .goods-name {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        margin-right: 3px;
      }
    }
  }

  .history-box {
    padding: 0 5px;

    .history-title {
      display: flex;
      justify-content: space-between;
      height: 40px;
      align-items: center;
      font-size: 13px;
      border-bottom: 1px solid #efefef;
    }

    .history-list {
      display: flex;
      flex-wrap: wrap;

      .uni-tag {
        margin-top: 5px;
        margin-right: 5px;
      }
    }
  }
</style>
